package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityDoorUserAddBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorUserQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorUserEditBo;
import com.ruoyi.system.domain.ZsyyCommunityDoorUser;
import com.ruoyi.system.mapper.ZsyyCommunityDoorUserMapper;
import com.ruoyi.system.vo.ZsyyCommunityDoorUserVo;
import com.ruoyi.system.service.IZsyyCommunityDoorUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 用户门禁管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityDoorUserServiceImpl extends ServicePlusImpl<ZsyyCommunityDoorUserMapper, ZsyyCommunityDoorUser> implements IZsyyCommunityDoorUserService {
    @Autowired
    private ZsyyCommunityDoorUserMapper iZsyyCommunityDoorUserMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyCommunityDoorUserVo queryById(Long id){
        return getVoById(id, ZsyyCommunityDoorUserVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityDoorUserVo> queryPageList(ZsyyCommunityDoorUserQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyCommunityDoorUserVo> page = iZsyyCommunityDoorUserMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyCommunityDoorUserVo> queryList(ZsyyCommunityDoorUserQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        return iZsyyCommunityDoorUserMapper.queryList(bo);
    }

    private LambdaQueryWrapper<ZsyyCommunityDoorUser> buildQueryWrapper(ZsyyCommunityDoorUserQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCommunityDoorUser> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.eq(bo.getCommunityId() != null, ZsyyCommunityDoorUser::getCommunityId, bo.getCommunityId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityDoorUserAddBo bo) {
        ZsyyCommunityDoorUser add = BeanUtil.toBean(bo, ZsyyCommunityDoorUser.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityDoorUserEditBo bo) {
        ZsyyCommunityDoorUser update = BeanUtil.toBean(bo, ZsyyCommunityDoorUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityDoorUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
